﻿'------AUTHOR---------DATE--------VERSION-----------------------NOTES----------------------------
'    Muhammed Hasip Timurtas    ' 05.03.2014 '    1.0    '     INITIAL CREATION FOR CORP SMS BENEFIT-
'------------------------------------------------------------------------------------------------
Imports ccapps.persistent.Telnet
Imports ccapps.persistent.Telnet.Scripting
Imports ccapps.persistent.FTP
Imports System.Globalization
Imports System.Threading
Imports ccapps.persistent.MenuAssigner
Imports ccapps.persistent.CorpSMS_Lib
Imports System.Data.OracleClient


Public Class CorpSMS_Option2_Benefit
    Inherits [lib].BasePage

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents ContentPaneFooter1 As ccapps.BillingNetControlLibrary.ContentPaneFooter
    Protected WithEvents Lblok As System.Web.UI.WebControls.Label
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents txtCoID As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtMsisdn As System.Web.UI.WebControls.TextBox
    Protected WithEvents trEffectiveOptions As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents trDealerCode As System.Web.UI.HtmlControls.HtmlTableRow


    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.PageTitle = "Kurumsal SMS Fayda İşlemleri"
        If Not Page.IsPostBack Then
            Lblok.Text = ""
            lblCustcode.Text = Request("CustCode")
            Me.fillForm()

            If Request("BENEFIT_DELETE") <> "" Then
                BenefitDelete_Delete()
            End If

        End If

    End Sub

    Public Shared Function GetBenefitTypes() As DataTable
        Dim sql As String
        sql = "SELECT * FROM billintra.corpsms_option2_benefit_types"
        Return persistent.myDBConn.myORATable(sql)
    End Function

    Private Sub fillForm()
        ddlBenefitType.DataTextField = "BENEFIT_TYPE"
        ddlBenefitType.DataValueField = "REC_ID"

        ddlBenefitType.DataSource = GetBenefitTypes()
        ddlBenefitType.DataBind()

        Me.ddlBenefitType.Items.Insert(0, New ListItem("Seçiniz", "0"))

        dg_Benefits.DataSource = Benefit_Definitions(Request("CustCode").ToString(), Request("OTP_ID").ToString())
        dg_Benefits.DataBind()
        txtBenefitAmonut.Value = ""

        If Request("PARENT_DELETE") = "" And Request("CHILD_DELETE") = "" Then
            Me.ddlBenefitType.SelectedIndex = 0
        End If
    End Sub

    Private Sub ContentPaneFooter1_Confirmed(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContentPaneFooter1.Confirmed
        Dim benefitAmount As String
        Dim processDate As String
        Dim operationType As String
        Dim result As String()

        Me.Lblok.Text = ""

        operationType = Me.ddlBenefitType.SelectedValue
        benefitAmount = txtBenefitAmonut.Value.Trim
        processDate = DateAndTime.Now.ToString("yyyy-MM-dd H:mm:ss")

        If benefitAmount = "" Then
            Me.Lblok.Text = "Fayda Miktarı Boş Geçilemez"
            fillForm()
            Return
        End If

        If ddlBenefitType.SelectedIndex = 0 Then
            Me.Lblok.Text = "Fayda Tipi Seçiniz"
            fillForm()
            Return
        End If

        If txtBenefitAmonut.Value.StartsWith("0") Then
            Me.Lblok.Text = "Miktar 0 olamaz"
            fillForm()
        End If

        Dim sql As String
        sql = "select max(rec_id) from billintra.corpsms_option2_benefit"
        Dim new_rec_id As Object = persistent.myDBConn.ORAScalar(sql)

        If IsDBNull(new_rec_id) Then
            new_rec_id = "0"
        End If


        If new_rec_id Is Nothing Then
            new_rec_id = "1"
        End If


        sql = "SELECT * FROM billintra.corpsms_option2_benefit_types WHERE  rec_id = " + ddlBenefitType.SelectedValue
        Dim b_dt As DataTable = persistent.myDBConn.myORATable(sql)
        Dim b_tax As String = CStr(b_dt.Rows(0).Item("TAX_RATE"))
        Dim b_sncode As String = CStr(b_dt.Rows(0).Item("SNCODE"))

        sql = "insert into billintra.corpsms_option2_benefit(rec_id,opt_id,opt_indicator,benefit_amount,benefit_type,tax_rate,custcode,occ_sncode,log_user,entry_Date) " +
       "values([TAG1],[TAG2],[TAG3],[TAG4],'[TAG5]',[TAG6],'[TAG7]',[TAG8],'[TAG9]',sysdate)"

        sql = sql.Replace("[TAG1]", Integer.Parse(new_rec_id) + 1)
        sql = sql.Replace("[TAG2]", Request("OTP_ID"))
        sql = sql.Replace("[TAG3]", "2") 'OPT_Indicator alanı 2 sabit. 
        sql = sql.Replace("[TAG4]", txtBenefitAmonut.Value)
        sql = sql.Replace("[TAG5]", ddlBenefitType.SelectedItem.Text)
        sql = sql.Replace("[TAG6]", b_tax)
        sql = sql.Replace("[TAG7]", Request("CustCode"))
        sql = sql.Replace("[TAG8]", b_sncode)
        sql = sql.Replace("[TAG9]", CStr(Session("user")))


        Try
            If persistent.myDBConn.ORAExecute(sql) > 0 Then
                Me.Lblok.Text = "Kayıt Yapıldı."
                Alert("Kayıt Yapıldı.", Me)
            Else
                Me.Lblok.Text = "Islem Gerceklesemedi. Hata Olustu, Tekrar Deneyiniz."
                Alert("Islem Gerceklesemedi. Tekrar Deneyiniz.", Me)
            End If
        Catch ex As Exception
            Me.Lblok.Text = "Islem Gerceklesemedi. Tekrar Deneyiniz. Hata : " + ex.Message
            Alert("Islem Gerceklesemedi. Tekrar Deneyiniz.", Me)
        End Try
        fillForm()

    End Sub

    Public Shared Function Benefit_Definitions(ByVal p_custCode As String, ByVal p_optId As String) As DataTable
        Dim sql As String
        sql = "SELECT opt_id,opt_indicator,benefit_amount,benefit_type,tax_rate,custcode,occ_sncode,rec_id FROM billintra.corpsms_option2_benefit   WHERE custcode = '[TAG1]' and opt_id = [TAG2] order by rec_id desc"
        sql = sql.Replace("[TAG1]", p_custCode)
        sql = sql.Replace("[TAG2]", p_optId)
        Return persistent.myDBConn.myORATable(sql)
    End Function



    Private Sub BenefitDelete_Delete()

        Try
            Dim sql As String
            Dim rec_id As String = Request("BENEFIT_DELETE")

            sql = "delete from  billintra.corpsms_option2_benefit WHERE  rec_id =" + rec_id
            persistent.myDBConn.ORAExecute(sql)
            Me.Lblok.Text = "Kayıt silme işlemi başarılı."
            Alert("Kayıt silme işlemi başarılı.", Me)
        Catch ex As Exception
            Me.Lblok.Text = "Islem Gerceklesemedi. Tekrar Deneyiniz. Hata : " + ex.Message
            Alert("Islem Gerceklesemedi. Tekrar Deneyiniz.", Me)
        End Try
        fillForm()

    End Sub

    Protected Sub dg_Benefits_ItemDataBound(sender As Object, e As DataGridItemEventArgs) Handles dg_Benefits.ItemDataBound
        If e.Item.ItemIndex >= 0 Then
            Dim hl_Delete As New System.Web.UI.WebControls.HyperLink

            hl_Delete.Text = "SIL"
            hl_Delete.NavigateUrl = "CorpSMS_Option2_Benefit.aspx?OTP_ID=" + Trim(e.Item.Cells(0).Text).Replace("&nbsp;", "") + "&CustCode=" + Trim(e.Item.Cells(5).Text).Replace("&nbsp;", "") + "&BENEFIT_DELETE=" + Trim(e.Item.Cells(7).Text).Replace("&nbsp;", "")
            e.Item.Cells(7).Controls.Clear()
            e.Item.Cells(7).Controls.Add(hl_Delete)

        Else
            e.Item.BackColor = System.Drawing.Color.Olive
        End If
    End Sub



End Class
